﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TomanuExtensions.Utils;

namespace AmiEmuLib.AmigaMachine.AmigaCPU
{
    /// <summary>
    /// Cache control registry.
    /// </summary>
    internal static class CACRRegister
    {
        public const int BIT_ENABLED = 1;
        public const int BIT_FROZEN = 2;
        public const int BIT_CLEAR_ENTRY = 4;
        public const int BIT_CLEAR = 8;

        public const uint MASK_ENABLED = 1 << BIT_ENABLED;
        public const uint MASK_FROZEN = 1 << BIT_FROZEN;
        public const uint MASK_CLEAR_ENTRY = 1 << BIT_CLEAR_ENTRY;
        public const uint MASK_CLEAR = 1 << BIT_CLEAR;

        /// <summary>
        /// Clear cache. Always zero.
        /// </summary>
        /// <param name="a_cacr"></param>
        /// <returns></returns>
        public static bool IsClear(uint a_cacr)
        {
            return false;
        }

        /// <summary>
        /// Clear cache entry.
        /// </summary>
        /// <param name="a_cacr"></param>
        /// <param name="a_bitValue"></param>
        public static void SetClear(ref uint a_cacr)
        {
        }

        /// <summary>
        /// Clear cache entry. Always zero.
        /// </summary>
        /// <param name="a_cacr"></param>
        /// <returns></returns>
        public static bool IsClearEntry(uint a_cacr)
        {
            return false;
        }

        /// <summary>
        /// Clear cache entry.
        /// </summary>
        /// <param name="a_cacr"></param>
        /// <param name="a_bitValue"></param>
        public static void SetClearEntry(ref uint a_cacr)
        {
        }

        /// <summary>
        /// Is cache frozen.
        /// </summary>
        /// <param name="a_cacr"></param>
        /// <returns></returns>
        public static bool IsFrozen(uint a_cacr)
        {
            return Bits.IsSet(a_cacr, BIT_FROZEN);
        }

        /// <summary>
        /// Set cache frozen state.
        /// </summary>
        /// <param name="a_cacr"></param>
        /// <param name="a_bitValue"></param>
        public static void SetFrozen(ref uint a_cacr, bool a_bitValue)
        {
            Bits.SetBit(ref a_cacr, BIT_FROZEN, a_bitValue);
        }

        /// <summary>
        /// Is cache enabled.
        /// </summary>
        /// <param name="a_cacr"></param>
        /// <returns></returns>
        public static bool IsEnabled(uint a_cacr)
        {
            return Bits.IsSet(a_cacr, BIT_ENABLED);
        }

        /// <summary>
        /// Enabled/disable cache.
        /// </summary>
        /// <param name="a_cacr"></param>
        /// <param name="a_bitValue"></param>
        public static void SetEnabled(ref uint a_cacr, bool a_bitValue)
        {
            Bits.SetBit(ref a_cacr, BIT_ENABLED, a_bitValue);
        }

        public static string Info(ushort a_cacr)
        {
            return String.Format("Frozen: {0}, Enable: {1}", IsFrozen(a_cacr), IsEnabled(a_cacr));
        }
    }
}
